<template>
  <div class="app-container">
    <el-card>
      <el-button type="success" @click="open_add_dialog">新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in list_c"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }">
          <el-button type="primary" size="mini" @click="open_edit_dialog(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini" @click="del_row(row)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px; text-align: right"
        @size-change="
          (val) => {
            this.pager.limit = val;
            this.getList();
          }
        "
        @current-change="
          (val) => {
            this.pager.page = val;
            this.getList();
          }
        "
        :current-page="pager.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      center
      :title="title"
      :visible.sync="visible"
      @close="visible = false"
    >
      <el-form :model="list_e" ref="list_e_ref" inline>
        <el-form-item prop="item" label="错误项" required>
          <el-input v-model="list_e.item" placeholder="错误项"></el-input>
        </el-form-item>
        <el-form-item prop="type" label="错误类型" required>
          <el-input v-model="list_e.type" placeholder="错误类型"></el-input>
        </el-form-item>
        <el-form-item prop="consequence" label="导致结果" required>
          <el-input
            v-model="list_e.consequence"
            placeholder="导致结果"
          ></el-input>
        </el-form-item>
        <el-form-item prop="method" label="预防手段" required>
          <el-input v-model="list_e.method" placeholder="预防手段"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList, handleAdd, handleEdit, handleDel } from '@/api_zc/mes/error_prevention_management'
export default {
  mounted () {
    this.getList()
  },
  data () {
    return {
      list: [],
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      list_c: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '错误项',
          prop: 'item'
        },
        {
          label: '错误类型',
          prop: 'type'
        },
      ],
      list_e: {
        item: null,
        type: null,
        consequence: null,
        method: null
      },
      visible: false,
      is_add: false,
      title: ''
    }
  },
  methods: {
    async getList () {
      const { data: { content, total } } = await getList({ page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    open_edit_dialog (row) {
      this.is_add = false
      this.visible = true
      this.title = "修改防错管理"
      this.$nextTick(() => {
        Object.assign(this.list_e, row)
      })
    },
    open_add_dialog () {
      this.is_add = true
      this.visible = true
      this.title = "添加防错管理"
      this.$nextTick(() => {
        this.$refs.list_e_ref.resetFields()
      })
    },
    async del_row (row) {
      await handleDel({ ids: [row.id] })
      this.$message.success('删除成功')
      this.getList()
    },
    submit () {
      this.$refs.list_e_ref.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请完善信息')
        }
        if (this.is_add) {
          await handleAdd(this.list_e)
          this.$message.success('添加成功')
          this.visible = false
          this.getList()
        } else {
          await handleEdit(this.list_e)
          this.$message.success('修改成功')
          this.visible = false
          this.getList()
        }
      })
    }
  }
}
</script>